package com.huonan.jb.tools.hdfsuse;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.cli.*;

import java.util.Arrays;
import java.util.StringJoiner;

@Slf4j
public class CommandArgs {
    private static Options options = new Options();


    static {
        init(options);
    }

    public static String getArgs(String[] args) {

        CommandLineParser parser = new DefaultParser();
        StringJoiner path = new StringJoiner("/");
        try {
            CommandLine cmd = parser.parse(options, args);
            if (cmd.getOptions().length < 5) {
                log.error("Not enough arguments");
                Arrays.stream(cmd.getOptions())
                        .forEach(a -> {
                            log.info("command:{},desc:{}", a.getLongOpt(), a.getDescription());
                        });
                System.exit(1);
            }

            String vendor = cmd.getOptionValue("vendor");
            String platform = cmd.getOptionValue("platform");
            String source = cmd.getOptionValue("source");
            String date = cmd.getOptionValue("date");
            path.add("/user/quantum_social/dataBackup/underlyingData/chengdu/das/product/download/success/");
            path.add(vendor);
            //path.add("regulation");
            path.add(date);
            path.add(platform);
            path.add(source);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return path.toString();
    }

    public static String getOptionValue(String[] args, String key) {
        CommandLineParser parser = new DefaultParser();
        CommandLine cmd;
        try {
            cmd = parser.parse(options, args);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return cmd.getOptionValue(key);

    }

    private static void init(Options options) {
        options.addRequiredOption("v", "vendor", true, "-v vend");
        options.addRequiredOption("p", "platform", true, "-p platform");
        options.addRequiredOption("s", "source", true, "-s source");
        options.addRequiredOption("d", "date", true, " -d date");
        options.addRequiredOption("l", "local_dir", true, "-l local_dir");
    }
}